<template>
  <div>
    <!-- 面包屑导航 -->
    <!--<Breadcrumb></Breadcrumb>-->
    <!-- 1. 产品搜索  -->
    <!--  
        el-form 表单
            :inline="true"      设置 inline 属性可以让表单域变为行内的表单域
            :model="formInline" 表单数据对象	object

        el-form-item 表单控件  每一项内容
            label	标签文本	string

        el-input 表单输入框

        el-date-picker 日期组件
          
    -->

    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="产品名称">
            <el-input v-model="formInline.name" @blur="blur" size="small" placeholder="产品名称"></el-input>
          </el-form-item>
          <el-form-item label="添加时间">
            <el-date-picker v-model="formInline.date" size="small" type="date" placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit" size="small">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button size="small" type="warning" icon="el-icon-plus" >添加商品</el-button>
        <el-button size="small" type="danger" icon="el-icon-delete" >批量删除</el-button>
      </div>
    </div>

    <!-- 2. 产品列表 -->
    <!-- 
        el-table 表格组件
            :data="tableData"  注入data对象数组 [{},{}]
            stripe属性可以创建带斑马纹的表格
             border 边框
            
        el-table-column 表格列
            prop="date"   prop属性来对应对象中的键名即可填入数据
            label="日期"  label属性来定义表格的列名
            width属性来定义列宽
        event：
          select	当用户手动勾选数据行的 Checkbox 时触发的事件	selection, row
          select-all	当用户手动勾选全选 Checkbox 时触发的事件	selection
     -->
  
  <div class="content">
  <el-table
    :data="tableData"
    border
    style="width: 100%"
    header-cell-class-name="active-header"
    cell-class-name="table-center"
    header-row-class-name="active-header"
    >
    <el-table-column
    type="selection"
    width="55"
    
    >
    </el-table-column>
    <el-table-column
      
      prop="id"
      label="商品编号"
      width="100">
    </el-table-column>
    <el-table-column
      show-overflow-tooltip
      prop="title"
      label="商品名称"
      width="120">
    </el-table-column>
    <el-table-column
      width="120"
      prop="price"
      label="商品价格">
    </el-table-column>
    <el-table-column
      width="120"
      prop="category"
      label="商品类目">
    </el-table-column>
    <el-table-column
      show-overflow-tooltip
      prop="create_time"
      label="添加时间">
      <template slot-scope="scope">
        {{ moment(scope.row.create_time).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
    </el-table-column>
    <el-table-column
      width="90"
      show-overflow-tooltip
      prop="sellPoint"
      label="商品卖点">
    </el-table-column>
    <el-table-column
      show-overflow-tooltip
      prop="descs"
      label="商品描述">
      <template slot-scope="scope">
        {{ removeHTMLTag(scope.row.descs) }}
      </template>
    </el-table-column>
    <el-table-column
      width="220"
      prop="address"
      label="操作">
        <template slot-scope="scope">
         <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)" 
              icon="el-icon-edit">编辑</el-button>
         <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)"
              icon="el-icon-delete">删除</el-button>
        </template>
  </el-table-column>
  </el-table>
  
  <div class="pagination"><Pagination :total="total" :pageSize="pageSize" @CurrentChange="CurrentChange"></Pagination></div>
  </div>



</div>
</template>

<script>
import Pagination from '@/components/pagination/Pagination.vue';
import moment from 'moment';
import { removeHTMLTag } from '@/utils/common.js'
export default {
components:{
  Pagination,
},
data() {
    return {
      formInline: {
        name: '',
        date: ''
      },
      tableData: [],
      total: 10,
      pageSize: 1,
      current: 1,//
      ids: [],//存储选中的id
    }
  },
  methods: {
    moment,
    removeHTMLTag,
    onSubmit(){
      console.log('submit!', this.formInline.name);
      this.search(this.formInline.name)
    },

    handleEdit(){},

    handleDelete(index, row){
      console.log('删除操作---', index, row);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //删除这个数据---请求后台接口----同步数据库-----------------
        this.deleteItemById(row.id)

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },


     //获取产品列表数据-------------------------------------
     async projectList(page) {
      let res = await this.$api.projectList({page})
      console.log('产品列表数据----', res.data);
      console.log('方法内page----',page);
      this.tableData = res.data.data;
      this.total = res.data.total;
      this.pageSize = res.data.pageSize;
    },

    //
    //删除接接口---------------------------------------
    async deleteItemById(id) {
      let res = await this.$api.deleteItemById({ id })
      console.log('删除----', res.data);
      if (res.data.status === 200) {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        //重新渲染视图-----------------------------
        //删除视图---如果当前是最后一页的最后一条数据-删除后--获取上一页的数据 
        //判断 this.total总数目   
        if (this.total % this.pageSize === 1) {
          this.current = this.current - 1 > 0 ? this.current - 1 : 1;//最好>1 
        }
        this.projectList(this.current)
      }
    },

    //失去焦点-------------------------------------------
    blur() {
      if (!this.formInline.name) {
        this.projectList(1)
      }
    },
    //查询
    async search(search) {
      if (!search) {//点击空的查询按钮
        return;
      }
      let res = await this.$api.search({ search })
      console.log('搜索数据--', res.data);
      if (res.data.status === 200) {//有数据
        this.tableData = res.data.result;
        //分页处理
        this.total = res.data.result.length;
        this.pageSize = res.data.result.length;
      } else {//查无数据
        this.tableData = []
        this.total = 1;
        this.pageSize = 1
      }
    },

    //页码
    CurrentChange(val){
      console.log('page----',val)
      this.current = val;
      this.projectList(val);
    },
  },
  created(){
    this.projectList(1);
  }
}
</script>

<style lang="less" scoped>
.header {
  background: #fff;
  margin-bottom: 20px;
  padding: 10px;

  .el-form-item {
    margin-bottom: 16px;
  }

  .group {
    border: 1px solid #eee;
    padding: 8px;
  }
}

.content {
  background: #fff;

  ::v-deep .active-header {
    color: #333;
    text-align: center;
  }

  ::v-deep .table-center {
    text-align: center;
  }

  .pagination {
    padding: 10px;
  }
}
</style>